import type { Course } from '@/models/course'
//一般的map转换成Course类型 不同时间段的课程拆成两个对象
export function toSeperatedCourse(courses: any) {
  const courseList: Course[] = []
  courses.forEach((val: any) => {
    const timeList = val.courseTimeList as any[]
    const teachers: string[] = val.teacher
    let teacher: string = ''
    let flag = 1
    if (teachers) {
      teachers.forEach((t: string) => {
        if (flag) {
          teacher += t
          flag = 0
        } else teacher = teacher + ',' + t
      })
    }
    timeList.forEach((time: any) => {
      const c: Course = {
        id: val.id,
        index: val.index,
        name: val.name,
        type: val.type,
        location: val.location,
        order: time.order,
        teacher: teacher,
        weekday: time.weekday,
        credit: val.credit,
        week: time.week,
        formatTime: getFormatCourseTime(time.week, time.weekday, time.order),
        academy: val.academy,
        comment: val.comment,
        finalWeight: val.finalWeight
      }
      courseList.push(c)
    })
  })
  return courseList
}
//一般的map转换成Course类型 不同时间段的课程合并
export function toMergedCourse(courses: any) {
  const courseList: Course[] = []
  courses.forEach((val: any) => {
    const timeList = val.courseTimeList as any[]
    const teachers: string[] = val.teacher
    let teacher: string = ''
    let flag = 1
    if (teachers) {
      teachers.forEach((t: string) => {
        if (flag) {
          teacher += t
          flag = 0
        } else teacher = teacher + ',' + t
      })
    }
    const weekdayList = [] as number[]
    const orderList = [] as number[]
    let formatTime = ''

    flag = 1
    timeList.forEach((val) => {
      weekdayList.push(val.weekday)
      orderList.push(val.order)
      if (!flag) formatTime += ';'
      else flag = 0
      formatTime += getFormatCourseTime(val.week, val.weekday, val.order)
    })

    const c: Course = {
      id: val.id,
      index: val.index,
      name: val.name,
      type: val.type,
      location: val.location,
      order: orderList.length == 1 ? orderList[0] : orderList,
      teacher: teacher,
      weekday: weekdayList.length == 1 ? weekdayList[0] : weekdayList,
      credit: val.credit,
      week: timeList.length > 0 ? timeList[0].week : [],
      formatTime: formatTime,
      academy: val.academy,
      comment: val.comment,
      finalWeight: val.finalWeight
    }
    courseList.push(c)
  })
  return courseList
}
export function getCourseType(type: number) {
  const courseType = ['compulsory', 'limited', 'unlimited']
  if (type >= 0 && type < courseType.length) {
    return courseType[type]
  }
  return null
}
//将某个课程的时间格式化
export function getFormatCourseTime(weeks: number[], weekday: number, order: number): string {
  const daysOfWeek: string[] = [
    '',
    '星期一',
    '星期二',
    '星期三',
    '星期四',
    '星期五',
    '星期六',
    '星期天'
  ]
  let result = ''
  let begin = 0
  let end = 0
  for (let i = 0; i < weeks.length; i++) {
    if (i === 0) {
      begin = weeks[i]
      end = weeks[i]
      continue
    }
    if (weeks[i] === end + 1) {
      end++
    } else if (weeks[i] !== end + 1) {
      if (end === begin) {
        result += `${begin},`
      } else {
        result += `${begin}~${end},`
      }
      begin = weeks[i]
      end = weeks[i]
    }
    if (i === weeks.length - 1) {
      if (end === begin) {
        result += `${begin}`
      } else {
        result += `${begin}~${end}`
      }
      result += `周，${daysOfWeek[weekday]}`
    }
  }
  result += '第' + order + '节'
  return result
}
